/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.screen;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.screen.BlessWords;
import com.boxin.ims.modules.wechat.service.screen.BlessWordsService;

/**
 * 祝福词Controller
 * @author Jakemanse
 * @version 2013-11-01
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/screen/blessWords")
public class BlessWordsController extends BaseController {

	@Autowired
	private BlessWordsService blessWordsService;
	
	@ModelAttribute
	public BlessWords get(@RequestParam(required=false) Long id) {
		if (id != null){
			return blessWordsService.get(id);
		}else{
			return new BlessWords();
		}
	}
	
	@RequiresPermissions("wechat:screen:blessWords:view")
	@RequestMapping(value = {"list", ""})
	public String list(BlessWords blessWords, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			blessWords.setUser(user);
		}
        Page<BlessWords> page = blessWordsService.find(new Page<BlessWords>(request, response), blessWords); 
        model.addAttribute("page", page);
		return "modules/wechat/screen/blessWordsList";
	}

	@RequiresPermissions("wechat:screen:blessWords:view")
	@RequestMapping(value = "form")
	public String form(BlessWords blessWords, Model model) {
		model.addAttribute("blessWords", blessWords);
		return "modules/wechat/screen/blessWordsForm";
	}

	@RequiresPermissions("wechat:screen:blessWords:edit")
	@RequestMapping(value = "save")
	public String save(BlessWords blessWords, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, blessWords)){
			return form(blessWords, model);
		}
		blessWordsService.save(blessWords);
		addMessage(redirectAttributes, "保存祝福词'" + blessWords.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/wechat/screen/blessWords/?repage";
	}
	
	@RequiresPermissions("wechat:screen:blessWords:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		blessWordsService.delete(id);
		addMessage(redirectAttributes, "删除祝福词成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/wechat/screen/blessWords/?repage";
	}

}
